package com.kj.xuyuan.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.WebviewBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;
import butterknife.BindView;

public class WebViewActivity extends BaseActivity {

    @BindView(R.id.webVi_message_details)
    TopProgressWebView webViMessageDetails;

    private int type;//3 用户协议  4 隐私政策 5 关于兑换  其他：问题或者文章的id，接口直接使用
    private String title;


    public static void actionStar(Context context,int type){
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type",-1);

        if(type == -1){
            Toast.makeText(context,"数据错误",Toast.LENGTH_SHORT).show();
            finish();
        }
        switch (type){
            case 3:
                title = "用户协议";
                setTitleText(title);
                break;
            case 4:
                title = "隐私政策";
                setTitleText(title);
                break;
            case 5:
                title = "关于兑换";
                setTitleText(title);
                break;
        }


        getAgreement();
    }

    private void getAgreement() {
        Map map = new HashMap();
        map.put("id",type+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.GET_WEB_TEXT, map, new JsonCallback<BaseModel<WebviewBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<WebviewBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        webViMessageDetails.loadTextContent(response.body().getData().getContent());
                        if(type != 3 && type != 4 && type != 5){
                            setTitleText(response.body().getData().getTitle());
                        }
                    }else{
                        ToastUtil.showToast(getContext(),response.body().getMsg());
                        finish();
                    }

                }
            }

            @Override
            public void onError(Response<BaseModel<WebviewBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}